home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / BINDER.HPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  8KB  |  279 lines

  1. /*
  2.     binder.hpp -- Loose Data Binder v 1.7:
  3.         container class.
  4.  
  5.     (C) Copyright 1992  John W. Small
  6.     All rights reserved
  7.  
  8.     PSW / Power SoftWare
  9.     P.O. Box 10072
  10.     McLean, Virginia 22102 8072 USA
  11.     (703) 759-3838
  12. */
  13.  
  14.  
  15. #ifndef Binder_HPP
  16. #define Binder_HPP
  17.  
  18.  
  19. #include <limits.h>    // UINT_MAX
  20. #include <iostream.h>
  21. #include <iomanip.h>
  22.  
  23.  
  24.  
  25. /*  Binder pointer types  */
  26.  
  27. typedef void *     voiD;
  28. #define voiD0    ((voiD)0)
  29. typedef voiD *     voiDV;
  30. #define voiDV0   ((voiDV)0)
  31.  
  32. typedef int      (* BDRcomP)(const voiD D1, const voiD D2);
  33. #define BDRcomP0 (( BDRcomP)0)
  34. typedef void     (* BDRapplY)(voiD D, voiD M, voiD A);
  35. typedef int      (* BDRdetecT)(voiD D, voiD M);
  36.  
  37. typedef class Binder * BindeR;
  38. #define BindeR0  ((BindeR)0)
  39.  
  40. typedef voiD (*BDRsloaD)(istream& is, voiD thiS);
  41. typedef BindeR * initVFTs;  //mk constructor unique
  42. #define initVFTsOnly    ((initVFTs)0)
  43.  
  44.  
  45.  
  46. /*  Binder constants  */
  47.  
  48. #define BDR_MAXNODES    ((unsigned)(UINT_MAX/sizeof(voiD)))
  49. #define BDR_LIMIT    20U
  50. #define BDR_DELTA    10U
  51. #define BDR_NOTFOUND    BDR_MAXNODES
  52.  
  53. #define BDR_SORTED    0x01U
  54. #define BDR_BIND_ONLY    0x00U
  55. #define BDR_DASSIGN     0x02U
  56. #define BDR_DNEW        0x04U
  57. #define BDR_DDELETE    0x08U
  58. #define BDR_DSTORE    0x10U
  59. #define BDR_ALL_FLAGS    0xFFU
  60.  
  61.  
  62.  
  63. class Binder {
  64.  
  65.  
  66. private:
  67.  
  68.     unsigned lowLimit, lowThreshold, first;
  69.     voiDV    linkS;
  70.     unsigned limit,    delta,   nodes;
  71.     unsigned maxNodes, curNode, flags;
  72.     BDRcomP  comP;
  73.  
  74.     static   Binder&  comPv;
  75.     static   unsigned comPID(BDRcomP comP);
  76.     static   BDRcomP  comPLU(unsigned ID);
  77.  
  78.     int initData(unsigned flags,
  79.             unsigned maxNodes,
  80.             unsigned limit,
  81.             unsigned delta);
  82.  
  83.  
  84. protected:
  85.  
  86.     Binder  (initVFTs) {}
  87.     void    destruct();
  88.     virtual voiD Dassign(voiD, const voiD)
  89.             { return voiD0; }
  90.     virtual voiD Dnew(const voiD)  { return voiD0; }
  91.     virtual void Ddelete(voiD D)  { delete (voiD) D; }
  92.     virtual int  Dattach(voiD)  { return 1; }
  93.     virtual void Ddetach(voiD)  { return; }
  94.     static  void sberror(const char * msg);
  95.     virtual void berror(const char * msg);
  96.     virtual void Dstore(ostream&, voiD) {}
  97.     virtual voiD Dload(istream&) { return voiD0; }
  98.     virtual void store(ostream& os);
  99.     static  BindeR load(istream& is, BindeR thiS);
  100.     int     vload(const char * filename,
  101.             BDRsloaD sloaD, voiD thiS);
  102.  
  103.  
  104. public:
  105.  
  106.     static  int   streamDebug;
  107.     static  char  memberTermChar;
  108.     static  void  RegisterComP(BDRcomP comP);
  109.     static  void  ForgetComPs()  { comPv.allDel(); }
  110.  
  111.  
  112. /*  Constructors and destructor  */
  113.  
  114.     Binder  (unsigned flags = BDR_BIND_ONLY,
  115.         unsigned maxNodes = BDR_MAXNODES,
  116.         unsigned limit = BDR_LIMIT,
  117.         unsigned delta = BDR_DELTA)
  118.         { (void) initData(flags,maxNodes,
  119.             limit,delta); }
  120.     Binder  (voiDV argv, unsigned argc = 0,
  121.         unsigned flags = BDR_BIND_ONLY);
  122.     Binder  (const char *filename)
  123.         { (void) vload(filename,(BDRsloaD)Binder::
  124.             load,this); }
  125.     int     save(const char *filename);
  126.     voiDV   vector();
  127.     virtual ~Binder()  { destruct(); }
  128.  
  129.     /*
  130.         You must override this destructor in any
  131.         descendant that overrides Ddelete() or
  132.         Ddetach(), i.e.
  133.  
  134.         virtual ~DerivedFromBinder()
  135.             { Binder::destruct(); }
  136.  
  137.         This is because base destructors are called
  138.         after vfts are reset to their default values
  139.         for the base level.  This means that
  140.         Binder::~Binder() calls Binder::Ddetach()
  141.         and perhaps Binder::Ddelete() instead of
  142.         the intended DerivedFromBinder::Ddetach()
  143.         or DerivedFromBinder::Ddelete().
  144.     */
  145.  
  146.  
  147. /*  Housekeeping Primitives  */
  148.  
  149.     unsigned Limit()  { return limit; }
  150.     unsigned setLimit(unsigned newLimit);
  151.     unsigned pack()  { return setLimit(nodes); }
  152.     unsigned Delta()  { return delta; }
  153.     unsigned setDelta(unsigned newDelta = BDR_DELTA);
  154.     unsigned Nodes()  { return nodes; }
  155.     unsigned MaxNodes()  { return maxNodes; }
  156.     unsigned setMaxNodes(unsigned newMaxNodes
  157.             = BDR_MAXNODES);
  158.     unsigned vacancy()  { return maxNodes - nodes; }
  159.     unsigned vacancyNonElastic()
  160.             { return limit - nodes; }
  161.     unsigned Flags(unsigned flags = BDR_ALL_FLAGS)
  162.             { return (this->flags & flags); }
  163.     unsigned setFlags(unsigned flags)
  164.             { return (this->flags |= flags); }
  165.     unsigned resetFlags(unsigned flags)
  166.             { return (this->flags &= ~flags); }
  167.     Binder&  operator<<(Binder& (*manipulator)(Binder&))
  168.             { return (manipulator?
  169.                 (*manipulator)(*this)
  170.                 : *this); }
  171.  
  172.  
  173.  
  174. /*  Elastic Array Primitives  */
  175.  
  176.     voiD     atIns(unsigned n, voiD D);
  177.     voiD     atInsNew(unsigned n, const voiD D);
  178.     voiD     atRmv(unsigned n);
  179.     void     allRmv();
  180.     int      atDel(unsigned n);
  181.     voiD     atDelAsg(unsigned n, voiD D);
  182.     int      allDel();
  183.     voiD     atPut(unsigned n, voiD D);
  184.     voiD     atPutNew(unsigned n, const voiD D);
  185.     voiD     atPutAsg(unsigned n, const voiD D);
  186.     voiD     atGet(unsigned n);
  187.     voiD     operator[](unsigned n)
  188.             { return atGet(n); }
  189.     voiD     atGetAsg(unsigned n, voiD D);
  190.     voiD     atXchg(unsigned n, voiD D);
  191.     unsigned index(const voiD D);
  192.     int      forEach(BDRapplY B,
  193.             voiD M = voiD0,    voiD A = voiD0);
  194.  
  195.  
  196. /*  Stack - Deque - Queue Primitives  */
  197.  
  198.     voiD     push(voiD D)  { return atIns(0,D); }
  199.     voiD     pushNew(const voiD D)
  200.             { return atInsNew(0,D); }
  201.     voiD     pop()  { return atRmv(0); }
  202.     Binder&  operator>>(voiD& D)
  203.             { D = atRmv(0); return *this; }
  204.     int      popDel()  { return atDel(0); }
  205.     voiD     popDelAsg(voiD D)
  206.             { return atDelAsg(0,D); }
  207.     voiD     top()  { return atGet(0); }
  208.     voiD     topAsg(voiD D)  { return atGetAsg(0,D); }
  209.     voiD     insQ(voiD D)  { return atIns(nodes,D); }
  210.     Binder&  operator<<(voiD D)
  211.             { atIns(nodes,D); return *this; }
  212.     voiD     insQNew(const voiD D)
  213.             { return atInsNew(nodes,D); }
  214.     voiD     unQ()  { return atRmv(nodes-1); }
  215.     int      unQDel()  { return atDel(nodes-1); }
  216.     voiD     unQDelAsg(voiD D)
  217.             { return atDelAsg(nodes-1,D); }
  218.     voiD     rear()  { return atGet(nodes-1); }
  219.     voiD     rearAsg(voiD D)
  220.             { return atGetAsg(nodes-1,D); }
  221.  
  222.  
  223. /*  List (single and double linked) Primitives  */
  224.  
  225.     unsigned CurNode();
  226.     int      setCurNode(unsigned n = BDR_MAXNODES);
  227.     voiD     ins(voiD D);
  228.     voiD     insNew(const voiD D);
  229.     voiD     rmv();
  230.     int      del();
  231.     voiD     delAsg(voiD D);
  232.     voiD     put(voiD D)  { return atPut(curNode,D); }
  233.     voiD     putNew(const voiD D)
  234.             { return atPutNew(curNode,D); }
  235.     voiD     putAsg(const voiD D)
  236.             { return atPutAsg(curNode,D); }
  237.     voiD     get()  { return atGet(curNode); }
  238.     voiD     getAsg(voiD D)
  239.             { return atGetAsg(curNode,D); }
  240.     voiD     next();
  241.     voiD     operator++()  { return next(); }
  242.     voiD     nextAsg(voiD D);
  243.     voiD     prev();
  244.     voiD     operator--()  { return prev(); }
  245.     voiD     prevAsg(voiD D);
  246.  
  247.     voiD     firstThat(BDRdetecT B, voiD M = voiD0);
  248.     voiD     lastThat(BDRdetecT B, voiD M = voiD0);
  249.  
  250.  
  251. /*  Priority Q, Set, Bag, Dictionary, Sort Primitives  */
  252.  
  253.     unsigned Sorted()  { return (flags & BDR_SORTED); }
  254.     void     unSort()  { flags &= ~BDR_SORTED; }
  255.     void     setComP(BDRcomP comP = BDRcomP0)
  256.             { this->comP = comP;
  257.             flags &= ~BDR_SORTED; }
  258.     BDRcomP  ComP()  { return comP; }
  259.     int      sort(BDRcomP comP = BDRcomP0);
  260.     voiD     insSort(voiD D);
  261.     voiD     insSortNew(const voiD D);
  262.     voiD     insUnique(voiD D);
  263.     voiD     insUniqueNew(const voiD D);
  264.     voiD     findFirst(const voiD K);
  265.     voiD     findNext(const voiD K);
  266.     voiD     findLast (const voiD K);
  267.     voiD     findPrev(const voiD K);
  268.     unsigned findAll(const voiD K);
  269.  
  270.  
  271.  
  272. };    /*  class Binder  */
  273.  
  274.  
  275. extern ostream& BDRendm(ostream& os);
  276. extern istream& BDRnextm(istream& is);
  277.  
  278. #endif  /*  Binder_HPP  */
  279.